package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.Record;
	import engine.logic.login.PlayCDTimerLogic;
	import engine.logic.tips.TipsManage;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.tip.tipItem.BookKungfuItem;
	import engine.ui.lite.BaseUI;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.text.NxLabel;
	import newx.com.control.window.NxWindowEvent;

	public class StatusShopUI extends BaseUI
	{
		private var _bindManage			:BindManage;
		private static var _self			:StatusShopUI;
		
		public var itemGroup				:NxViewGroup;
		
		private var _model					:ModelLocator;
		
		//public var lookStatueBtn			:NxButton;
		
		/**左右边背景框*/
		public var leftBack				:MovieClip;
		public var rightBack				:MovieClip;	
		public var bg_right				:MovieClip;
		public var BackLine				:MovieClip;
		
		//public var effect					:MovieClip;
		/**文本*/
		//public var text_1					:NxText;
		/**属性升级爵位选择Tab*/
		//public var itemTab					:NxTab;
		
		/**属性升级界面当前页签*/
		private var itemTabList			:int		=	-1;
			
		private var _skillUpDataArr		:Array;
		
		/**当前爵位*/
		public  var statusLevel			:NxLabel;
		
		/**声望值*/
		public  var statusExp				:NxLabel;
		
		/**得到声望说明*/
		public  var getLab					:NxLabel;
		
		/**清除CD按钮*/
		//public  var getBtn					:NxButton;
		
		/**爵位图标*/	
		public  var statusClip				:NxClip;
			
		private var _clearCDBtnNum			:int	=	4;
		/**技能CD倒计时*/
		private var _skillOverTimer		:Timer	=	new Timer(1000);
			
		private	var _bindShortCut			:int	=	-1;
		private var _bindCDtime			:int	=	-1;
		private var _bindExploitIndex		:int	=	-1;
		private var _bindLevelIndex		:int	=	-1	
			
		/**最短CD信息*/
		private var minCD					:Array;
		
		/**界面是否打开*/
		private var _isOpen				:Boolean;
		
		/**所有按钮文本*/
		private var allUpText				:Array	=	new Array()	;
		
		/**所有按钮*/
		private var allUpBtnArr			:Array;
		
		/**所有鼠标触碰特效*/
		private var allMouseOverArr		:Array;
		
		/**技能容器数组*/
		private var _infoArr:Array;
		
		/**表格的最大个数*/
		private var _count 				:int;
		
		/**链接声望文本*/
		private var test					:TextField	=	new TextField();
		
		/**最短时间*/
		private var _minTime				:int;
		
		/**下一页*/
		public var nextBtn					:NxButton;
		
		private var _myLevel				:int;
		
		//public var chakan					:NxButton
		
		public var lefbtn					:NxButton;
		
		public var yeqiandi				:MovieClip;
		
		public var yema					:NxLabel;
		
		/**被选中技能的index*/
		private var _mousePlace			:int;
		
		/**是否有可以升级的技能*/
		private var _isCanUp				:Boolean	=	false;
		
		public function StatusShopUI(popupUI:Boolean=true)
		{
			super(popupUI);
		}
		
		public static function getInstance():StatusShopUI
		{
			if(_self == null)
			{	
				_self	=	new StatusShopUI;
			}
			return _self;
		}
		override public function Init():void
		{
			_count		 		=	itemGroup.row * itemGroup.col;
			allUpBtnArr			=	[];
			allMouseOverArr		=	[];
			_infoArr			=	[];
			yema.visible		=	false;
			yeqiandi.visible	=	false;
			test.visible		=	false;
			itemTabList			=	1;
			windowBG.addChild(test);
			_model				=	ModelLocator.getInstance();
			_bindManage			=	BindManage.getInstance();	
			if( _bindShortCut == -1 )
			{
				_bindShortCut		=	_bindManage.BindRecord(_model.scene,"PeeragePropRec",onAddShortCutKey,onRemoveShortCutKey);
			}
			if( _bindExploitIndex == -1 )
			{
				_bindExploitIndex	=	_bindManage.BindProp(_model.scene.Role, "Exploit", this, "point");
			}
		}
		
		/**
		 *初始化viewGroup 
		 * 
		 */		
		private function initNxViewGroup():void
		{
			var img 	: NxClip;
			//var count 	: int = 6;
			var itemMC 	: MovieClip;
			for( var i : int = 0; i < _count; i++ )
			{
				itemMC = itemGroup.getItemByIndex(i);
				itemMC["backMc"].gotoAndStop(1);
				allMouseOverArr.push(itemMC["backMc"]);
				img = new NxClip();
				img.defaultSkin = "NotFound6";
				img.isCache 	= true;
				itemMC.addChild( img );			
				itemMC["clip"] 		= 	img;
				itemMC["index"] 	= 	i;
				itemMC.visible		=	false;
				itemMC.addEventListener( MouseEvent.ROLL_OVER,showSkillUpTips );
				itemMC.addEventListener( MouseEvent.ROLL_OUT,onCloseTips );
			}		
			itemGroup.addItemsEvent( MouseEvent.ROLL_OUT, onCloseTips );
		}
		
//		override public function show(space:DisplayObjectContainer=null, onshow:Function=null, ...argc) : void
//		{
//			if( _model.scene.Role.GetPropertiesEx("Level") < 10 )
//			{
//				//AlertUI.showAlert(StaticIni.getSwfLangStr("UIStr_1000827"),StaticIni.getSwfLangStr("UIStr_1001818") );
//				return;
//			}
//			super.show();
//		}
			
		
		override protected function BeforeShow():void
		{
			//this.effect.gotoAndPlay(1);
			if( null != parent )return;
			initNxViewGroup();
			allUpBtnArr	 =	[];
			_myLevel 	 =	_model.scene.roleLevel;		
			_isOpen				=	true;
			_mousePlace			=	-1;
			//在CD模块注册	
			//PlayCDTimerLogic.getInstance().resgis(2,setTimeCd);
			if( _bindLevelIndex == -1 )
			{
				_bindLevelIndex	=	_bindManage.BindProp(_model.scene.Role, "Level", this, "onLevelhandle");
			}
			_bindShortCut	=	_bindManage.UnbindRecord("PeeragePropRec",_bindShortCut);
			if( _bindShortCut == -1 )
			{
				_bindShortCut		=	_bindManage.BindRecord(_model.scene,"PeeragePropRec",onAddShortCutKey,onRemoveShortCutKey);
			}
			addEvent();
		}
		
		override protected function AfterHide():void
		{
			//_bindShortCut		=	_bindManage.UnbindRecord( "PeeragePropRec", _bindShortCut );
			//_bindExploitIndex	=	_bindManage.UnbindProp( _model.scene.Role, "Exploit", _bindExploitIndex );
			_bindExploitIndex	=	_bindManage.UnbindProp( _model.scene.Role, "Level", _bindExploitIndex );
			_isOpen				=	false;
			windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE, onClickClose);	
			removeEvent();
		}
								
		private function onAddShortCutKey(key:String,rowValue:*,data:*):void
		{		
			addAttributeType();
			isCanUpStatus();
		}
		private function onRemoveShortCutKey(key:String,rowValue:*,data:*):void
		{
		}
	
		/**
		 *  添加侦听事件
		 */		
		private function addEvent():void
		{
			nextBtn.addEventListener(MouseEvent.CLICK,onChangeType);
			lefbtn.addEventListener(MouseEvent.CLICK,onChangeType);
			windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClickClose);
		}
		
		private function removeEvent():void
		{
			nextBtn.removeEventListener(MouseEvent.CLICK,onChangeType);
			lefbtn.removeEventListener(MouseEvent.CLICK,onChangeType);
//			chakan.removeEventListener(MouseEvent.CLICK,onReceive);
		}
		
		/**
		 * 页签点击事件
		 * @param e
		 */		
		private function onChangeType(e:MouseEvent):void
		{
//			var num:Number	=	e.target
//			itemTabList		=	num+1;
			var btn:NxButton	=	e.target as NxButton;
			nextBtn.visible	=	true;
			lefbtn.visible	=	true;
			if(btn.skin	==	"NxButton_001")
			{
				itemTabList--;
				if(itemTabList < 1)
				{
					itemTabList	=	1;
				}
			}
			else
			{
				itemTabList++;
				if(itemTabList > _infoArr.length)
				{
					itemTabList	=	_infoArr.length;
				}
			}
			addAttributeType();
		}
		
		/**
		 * 检测阅历是否足够 
		 * @param arr
		 * 
		 */		
		private function enoughMoney(arr:Array):Boolean
		{
			//阅历值
			var roleExploit:int		=	_model.scene.Role.GetProperties("Exploit");
			
			//升级需要的阅历值
			var addStr:String	=	String(arr[2] + "=" + String(arr[0]));
			var typeStr:String	=	StaticIni.getIniVar(addStr, String(arr[1] + 1));
			var typeArr	:Array	=	typeStr.split(",");
			var needExp	:int	=	int(typeArr[3]);	
			
			if(roleExploit > needExp)
			{
				return true;
			}
			return false;
		}

		/**
		 * tips 
		 * @param e
		 * 
		 */		
		private function showSkillUpTips(e:MouseEvent):void
		{
			var itemMC:MovieClip	=	e.currentTarget as MovieClip;
			var arr:Array		=	itemMC.configID;
			//_mousePlace			=	e.currentTarget.index+1;
			for(var i:int = 0;i < allMouseOverArr.length;i++)
			{
				(allMouseOverArr[i] as MovieClip).gotoAndStop(1);
			}
			_mousePlace			=		arr[0];
			itemMC["backMc"].gotoAndStop(4);
			//tipsShow(arr);
		}
		
		/**
		 *tips
		 *  
		 */
		private function tipsShow(arr:Array):void
		{
			//
			if(arr[0]!= _mousePlace)return
			var roleExploit	:int	=	_model.scene.Role.GetPropertiesEx("Exploit");
			
			//当前爵位类型
			var peerageT:int	=	_model.scene.Role.GetPropertiesEx("PeerageRank");
			
			//	var arr:Array		=	e.currentTarget.configID;
			//现在的伤害
			if(arr == null )return
			var nowkill:int		=	0;
			var addStr:String	=	String(arr[2] + "=" + String(arr[0]))
			//下一级信息
			var typeStr:String	=	StaticIni.getIniVar(addStr, String(arr[1] + 1));
			//当前等级信息
			if ( arr[1] >= 150)
			{
				str		=	StaticIni.getSwfLangStr("UIStr_1001147");
				TipsMainLogic.instance.showString( str );
				return;
			}
			if (arr[1] > 0)
			{
				var nowStr:String	=	StaticIni.getIniVar(addStr, String(arr[1]));
				var nowArr:Array	=	nowStr.split(",");
				nowkill				=	int(nowArr[0]);
			}	
			var typeArr	:Array	=	typeStr.split(",");
			var killNum:int		=	int(typeArr[0])-nowkill;
			var expend	:int	=	int(typeArr[1]);
			var needExp	:int	=	int(typeArr[3]);
			//var CDtime	:int	=	int(typeArr[4]);
			var str	:String		=	"";
			var strKill:String	=	StaticIni.getSwfLangStrVar(
										String("PeerageProTips_" + ((arr[0] - 1) % 20)), [killNum]);
			str					= 	str.concat(strKill);	
			
			var strNeedLv:String=	StaticIni.getSwfLangStrVar (
										String("PeerageProCost_2"), [needExp]);	
			if (needExp > roleExploit)
			{
				strNeedLv	=	"<font color='#f00000'>"+strNeedLv+"</font>";
			}							
			str				= str.concat("<br>");
			str				= str.concat(strNeedLv);		
			TipsMainLogic.instance.showString( str );
		}
		
		private function onCloseTips(e:MouseEvent):void
		{
			var itemMC:MovieClip	=	e.currentTarget as MovieClip;
			_mousePlace =	-1;
			for(var i:int = 0;i < allMouseOverArr.length;i++)
			{
				(allMouseOverArr[i] as MovieClip).gotoAndStop(1);
			}
		//	itemMC["backMc"].gotoAndStop(1);
			TipsMainLogic.instance.hide();
		}
		
		/**
		 *界面上面
		 *当前爵位信息
		 */		
//		private function statusType():void
//		{
//			var txtStyle:StyleSheet	=	new StyleSheet();
//				txtStyle.setStyle("a:link"	,{color:"#"+StaticIni.getIniVar("Css_txt_DAyActivity","link"),textDecoration:"underline"});
//				//txtStyle.setStyle("a:visited",{color:"#"+StaticIni.getIniVar("Css_txt_DAyActivity","link")});
//				txtStyle.setStyle("a:hover"	,{color:"#"+StaticIni.getIniVar("Css_txt_DAyActivity","hover"),textDecoration:"none"});	
//			test.styleSheet		=	txtStyle
//			//当前爵位类型
//			var peerageT:int	=	_model.scene.Role.GetPropertiesEx("PeerageRank");
//			var str:String;
//			str					=	StaticIni.getSwfLangStr( "PeerageName_" +  ( peerageT + 1) );
//			//statusExp.htmlText	=	"<b>"+StaticIni.getSwfLangStrVar("textsize",[16,str])+"</b>";		
//			var myExploit:int		=	_model.scene.Role.GetProperties("Exploit");
//			var linkStr	 :String	=	StaticIni.getSwfLangStr("PeerageStr_33");		
//			//test.addEventListener(TextEvent.LINK, onOpenGetStatus)
//			test.htmlText			=	("<a href=\"event:" + "\">" + linkStr +"</a>");
//			//text_1.htmlText			=	" "+StaticIni.getSwfLangStr("UIStr_1001812");
//		}
		
		/**	
		 * 属性升级信息
		 * 
		 */		
		private function addAttributeType():void
		{
			if(!_isOpen)return
			clearViewGroup();		
			var typeArr1:Record		=	_model.scene.GetProperties("PeeragePropRec");
			var arr:Array			=	typeArr1.recArr;
			var typeArr:Array		=	new Array();			
			var levelArr:Array		=	new Array();
			var itemMC:MovieClip;			
			//表格总个数
			//var maxViewNum:int		=	itemGroup.row * itemGroup.col;
			_myLevel				=	_model.scene.roleLevel;
			//if (_myLevel	< 10 ) return;	
			_infoArr	=	[];
			var tabNum:int	=	arr.length%_count > 0  ?  arr.length/_count+1:arr.length/_count;
			if( tabNum == 0 )return
			for (var k:int = 0; k < tabNum; k++) 
			{
				var tempArr:Array		=	new Array();
				for(var p:int = 0; p < _count; p++)
				{
					if(arr[k*_count+p] == null)break;
					tempArr.push(arr[k*_count+p]);
				}
				_infoArr.push(tempArr);
			}

			for( var i:int = 0 ; i < _infoArr[itemTabList-1].length ; i++ )
			{
				resetSkillUpItem(
					itemGroup.getChildAt(i) as MovieClip,
					_infoArr[itemTabList-1][i])
			}
			
			//如果要显示的技能大于表格的最大数，则显示翻页
			if(arr.length > _count)
			{
				nextBtn.visible	=	true;
				lefbtn.visible	=	true;
				yema.visible	=	true;
				yeqiandi.visible=	true;
			}
			
			if(itemTabList == 1)
			{
				lefbtn.visible	=	false;
			}
			if(itemTabList == _infoArr.length)
			{
				nextBtn.visible	=	false;
			}
			yema.text		=	itemTabList+"/"+_infoArr.length;
		}

		/**
		 * 重置升级列表中的某一项，配合ViewGroup_skillup.getItemForEach使用
		 * @param val
		 * 
		 */		
		private function resetSkillUpItem(val:MovieClip,pList:Array = null):void
		{
			if(pList== null)return;
			//技能名
			val.visible	=true;
			
			var tSkillNameTxt	:TextField		=	val["titleTxt0"];
			//学习等级
			var tSkillLevelTxt	:TextField		=	val["titleTxt1"];
			var updateBtnLbl	:TextField		=	val["titleTxt2"];//升级按钮
			
			var addbuf	:TextField				=	val["titleTxt3"];//增加效果
			var needExpoint	:TextField			=	val["titleTxt4"];//需要阅历
			
			//升级按钮
			var upaddBtn		:SimpleButton	=	val["btn"];
			//图片框
			var tSkillIcon		:MovieClip		=	val["img"];
			
			var addStr:String	=	String(pList[2] + "=" + String(pList[0]))
			//下一级信息
			var typeStr:String	=	StaticIni.getIniVar(addStr, String(pList[1] + 1));
			var typeArr	:Array	=	typeStr.split(",");
			
			var roleExploit	:int	=	_model.scene.Role.GetPropertiesEx("Exploit");
			var needExp:int			=	int(typeArr[3]);
			var needExpStr:String	=	StaticIni.getSwfLangStrVar("PeerageProCost_2", [needExp]);
			if (needExp > roleExploit)
			{
				needExpStr	=	"<font color='#f00000'>"+needExpStr+"</font>";
			}	
			
			var nowkill:int		=	0;
			if (pList[1] > 0)
			{
				var nowStr:String	=	StaticIni.getIniVar(addStr, String(pList[1]));
				var nowArr:Array	=	nowStr.split(",");
				nowkill				=	int(nowArr[0]);
			}	

			var killNum:int		=	int(typeArr[0])-nowkill;
			
			var strKill:String	=	StaticIni.getSwfLangStrVar(
										String("PeerageProTips_" + ((pList[0] - 1) % 20)), [killNum]);
			//读配置文件
			var attr:String 					= 	StaticIni.getIniVar("PeerageProperty_" + (pList[0]-1), "name");
		
			val.configID						=	pList;
			tSkillNameTxt.htmlText				=	StaticIni.getSwfLangStr(attr);	
			tSkillLevelTxt.htmlText				=	StaticIni.getSwfLangStrVar("PeerageProLevel_1", [pList[1]]);
			updateBtnLbl.htmlText				=	StaticIni.getSwfLangStr("BaiShiUIStr_10");//  			
			needExpoint.htmlText				=	needExpStr;
			addbuf.htmlText						=	strKill;
			needExpoint.mouseEnabled 			=	false;
			updateBtnLbl.mouseEnabled 			=	false;
			addbuf.mouseEnabled 				=	false;
			if(pList[1] >= _myLevel)
			{
				upaddBtn.visible				=	false;
				updateBtnLbl.htmlText			=	StaticIni.getSwfLangStr("UIString_1000316");
			}
			else
			{
				upaddBtn.visible	=	true;
			}
			upaddBtn.addEventListener(MouseEvent.CLICK, pressSkillUp);
			var viewPic :NxClip		=	val["clip"];
			if(viewPic	==	null)return
			viewPic.source	=	StageInfo.jpgResPath + StaticIni.getIniVar("PeerageProperty_" + (pList[0]-1), "imgSrc");
			
			tSkillIcon.addChild(viewPic);
			val.visible			=	true;
			//allUpText.push(tSkillBtnTxt);
			allUpBtnArr.push(upaddBtn);
			//tipsShow(pList);
		}
				
		/**
		 *升级按钮点击 
		 */		
		private function pressSkillUp(e:MouseEvent):void
		{
			//var num:Array	=	(e.target.parent as MovieClip).configID ;
			SystemSender.getInstance().CustomMsg(
				CustomToServerFlag.CLIENT_CUSTOMMSG_UPGRADE_PEERAGEPROP,_mousePlace);			
		}
		
		/**
		 *  清空指定图片组
		 * @param viewGroupType 所要清空的图片组
		 * 
		 */		
		private function clearViewGroup():void
		{
			//当前图片组最大显示数
			var maxNmu:int	=	itemGroup.row * itemGroup.col;
			for(var i:int = 0; i < maxNmu; i++)
			{
				var viewItem:MovieClip				=	itemGroup.getItemByIndex(i) as MovieClip;
				viewItem. data						=	null;				
				viewItem. titleTxt0.htmlText		=	"";
				viewItem. titleTxt1.htmlText		=	"";
				viewItem. titleTxt2.htmlText		=	"";
				viewItem. titleTxt0.mouseEnabled 	=	false;
				viewItem. titleTxt1.mouseEnabled	=	false;
				viewItem. titleTxt2.mouseEnabled	=	false;
				viewItem.visible					=	false;
				while(viewItem.img.numChildren>0)
				{
					viewItem.img.removeChildAt(0);
					viewItem["Data"]	=	null;
				}
			}
		}
		
		/**
		 *绑定人物等级 
		 * @param val
		 * 
		 */		
		public function set onLevelhandle(val:String):void
		{
			_myLevel 	 =	_model.scene.roleLevel;
			//addAttributeType();
		}
		/**
		 * 
		 * 功勋货币绑定处理
		 */
		public function set point(val:String):void
		{
			var myExploit:int		=	_model.scene.Role.GetProperties("Exploit");
			var str		 :String	=	StaticIni.getSwfLangStrVar("PeerageStr_25", [myExploit]);	
			statusLevel.htmlText	=	str;
			test.x	=	statusLevel.x+statusLevel.width+10;
			test.y	=	statusLevel.y;
			isCanUpStatus();
			addAttributeType();
		}

		/**
		 *关闭界面
		 */
		private function onClickClose(e:NxWindowEvent):void
		{
			//this.requestClose();
			this.hide();
		}
		
		/**
		 *升级不可点击
		 */
		private function isUpstatus():void
		{
			for (var i:int = 0; i < allUpText.length; i++) 
			{
				(allUpText[i] as TextField).htmlText	=	StaticIni.getSwfLangStr("PeerageStr_28");
				(allUpBtnArr[i] as SimpleButton).visible	=	false;
			}
		}
		
		/**
		 *升级可点击
		 */
		private function isUpcan():void
		{
			for (var i:int = 0; i < allUpText.length; i++) 
			{
				(allUpText[i] as TextField).htmlText	=	StaticIni.getSwfLangStr("UIStr_1001146");
				(allUpBtnArr[i] as SimpleButton).visible	=	true;
			}
		}
		
		/**
		 *最短cd时间，由CDtimeLogic发来数据
		 */
		public function setTimeCd(time:int,col:int):void 
		{
			if(time <= 0)
			{
				//getBtn.visible	=	false;
				//lab_CD.htmlStr 	= 	"";
				isUpcan();
				return;
			}
			isUpstatus();
			//getBtn.visible	=	true;
			_minTime		=	time;
			//lab_CD.htmlStr =  StaticIni.getSwfLangStrVar("UIStr_1001816", [getDHMS(time)[1],getDHMS(time)[2]]);// "冷确" + ":" +m + ":" + s;
		}
		/**
		 *关闭界面
		 */
		public function isOpenUI():Boolean
		{
			return _isOpen;
		}
		
		/**
		 * 遍历所有看是否有可以升级的技能 
		 * @return 
		 * 
		 */		
		private function  isCanUpStatus():void
		{
			if(_model.scene.GetProperties("PeeragePropRec") == null
				|| _model.scene.Role.GetProperties("Exploit")==null )return;
			var typeArr1:Record		=	_model.scene.GetProperties("PeeragePropRec");
			var arr:Array			=	typeArr1.recArr;
			//var tempArr:Array		=	new Array();
			var _myLevel:int		=	_model.scene.roleLevel;
			_isCanUp 	=		false;
			for(var i:int = 0 ; i < arr.length ; i ++)
			{
				if(enoughMoney(arr[i]))
				{
					_isCanUp =true;
				}
			}	
		}
		
		/**
		 *  
		 * @return 
		 * 
		 */		
		public function get checkGuide():int
		{
			if(!_isCanUp)
			{
				return -1;
			}
			else
			{
				return 1;
			}
		}
	}
}